package scala

import scala.io.Source

object Demo26Student2 {
  def main(args: Array[String]): Unit = {
    /**
      *
      * 3、统计每科都及格的学生
      *
      */


    //1、计算每隔科目及格的分数线

    val cources: List[String] = Source.fromFile("data/cource.txt").getLines().toList


    val couSco: Map[String, Double] = cources.map(cource => {
      val split: Array[String] = cource.split(",")
      val cid: String = split(0)
      //及格的分数线
      val sco: Double = split(2).toDouble * 0.6

      (cid, sco)
    }).toMap //转换成map 集合。  可以通过科目编号获取科目及格分数


    //2、读取分数表
    val scores: List[String] = Source.fromFile("data/score.txt").getLines().toList

    //将不及格的分数过滤
    val filter: List[String] = scores.filter(score => {
      val split: Array[String] = score.split(",")

      //科目编号
      val cid: String = split(1)
      //学生科目分数
      val sco: Double = split(2).toDouble

      //通过科目编号获取科目及格分数
      val sc: Double = couSco.getOrElse(cid, 0.0)

      //判断是否及格
      sco >= sc
    })


    /**
      * 统计学生及格的科目数
      */


    //取出学号
    val ids: List[String] = filter.map(sco => sco.split(",")(0))

    //按照学号分组

    val idGroup: Map[String, List[String]] = ids.groupBy(id => id)

    //统计及格的科目数
    val legId: Map[String, Int] = idGroup.map(kv => {
      val id: String = kv._1
      //j及格的科目数
      val length: Int = kv._2.length
      (id, length)
    })

    //取出都及格的学生
    val sumSocreStu: Map[String, Int] = legId.filter(kv => kv._2 == 6)

    sumSocreStu.foreach(println)


  }
}
